﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace ProjectDanmaku.UI
{
    public class FpsText : MonoBehaviour
    {
        private Text _text;
        private float _avgFrameTime = 0.01f;
        
        private void Awake()
        {
            _text = GetComponent<Text>();
        }

        private void Start()
        {
            StartCoroutine(FpsPresentCoroutine());
        }

        private void Update()
        {
            _avgFrameTime = (_avgFrameTime + Time.deltaTime) / 2f;
        }

        private IEnumerator FpsPresentCoroutine()
        {
            while (true)
            {
                _text.text = $"FPS: {1 / _avgFrameTime:0.00}";
                yield return new WaitForSeconds(0.1f);
            }
        }
    }
}